package DBLayer;
import ModelLayer.*;

import java.sql.*;
import java.util.ArrayList;

public class DBSupplier implements IFDBSupplier{
    private  Connection con;
    /** Creates a new instance of DBSupplier */
    public DBSupplier() {
      con = DbConnection.getInstance().getDBcon();
    }
    
  //Implements the methods from the interface
    // get all Klasses
    public ArrayList<Supplier> getAllKlasses(boolean retriveAssociation)
    {
    	
        return miscWhere("", retriveAssociation);
    }
    
    //get one Supplier having the holdId
    public Supplier findSupplier(int supplierId, boolean retriveAssociation)
    {   
    	String wClause = "  supplierId = '" + supplierId + "'";
        return singleWhere(wClause, retriveAssociation);
    }
    
    //find one Supplier having the supplierName
    public Supplier searchSupplierName(String supplierName, boolean retriveAssociation)
    {
        String wClause = "SupplierName like '%" + supplierName + "%'";
        System.out.println("SearchSupplier " + wClause);
        return singleWhere(wClause, retriveAssociation);
    }
   
     

	
	//private methods
    //michWere is used whenever we want to select more than one Supplier 
	private ArrayList<Supplier> miscWhere(String wClause, boolean retrieveAssociation)
	{
            ResultSet results;
	    ArrayList<Supplier> list = new ArrayList<Supplier>();	
		
	    String query =  buildQuery(wClause);
	    System.out.println("print noget ud " +query+"");
            try{ // read the supplier from the database
		Statement stmt = con.createStatement();
	 	stmt.setQueryTimeout(5);
	 	results = stmt.executeQuery(query);
	 	
	
		while( results.next() ){
	     	 Supplier suppObj = new Supplier();
		 suppObj = buildSupplier(results);	
                 list.add(suppObj);	
		}//end while
                 stmt.close();     
                 if(retrieveAssociation)
                 {   //The supervisor and department is to be build as well
                     for(Supplier suppObj : list){
                        int supplierId1 = suppObj.getSupplierId();
                        Supplier supplierId = singleWhere(" supplerId = '" + supplierId1 + "'",false);
                        suppObj.setSupplierId(supplierId);
                        System.out.println("SupplierId is seleceted");
                       // here the department has to be selected as well
                     }
                 }//end if   
			
		}//slut try	
	 	catch(Exception e){
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return list;
	}
	
	//Singelwhere is used when we only select one employee 	
	private Supplier singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		Supplier suppObj = new Supplier();
		
                
	        String query =  buildQuery(wClause);
                System.out.println(query);
		try{ // read the klasse from the database
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() ){
                            suppObj = buildSupplier(results);
                            //assocaition is to be build
                            stmt.close();
                            
			}
                        else{ 
                        	//no supplier was found
                            suppObj = null;
                        }
		}//end try	
	 	catch(Exception e){
	 		System.out.println("Query exception: "+e);
	 	}
		return suppObj;
	}
	

	//method to build the query
	private String buildQuery(String wClause)
	{
	    String query="SELECT supplierId, supplierName FROM Supplier";
		
		if (wClause.length()>0)
			query=query+" WHERE "+ wClause;
			
		return query;
	}
	
	
	//method to build an employee object
	private Supplier buildSupplier (ResultSet results)
      {   Supplier suppObj = new Supplier();
          try{ // the columns from the table emplayee  are used
                suppObj.setSupplierId(results.getInt("supplierId"));
                suppObj.setSupplierName(results.getString("supplierName"));
          }
         catch(Exception e)
         {
             System.out.println("error in building the employee object");
         }
         return suppObj;
      }

 
}